package com.wxm.jwt.client.util;

import com.wxm.jwt.client.properties.JWTAuthClient;
import com.wxm.jwt.exception.JWTException;
import com.wxm.jwt.util.JwtHelper;
import lombok.AllArgsConstructor;

import java.security.PublicKey;
import java.util.function.Supplier;

/**
 * <p>
 *      JwtToken 客户端工具
 * </p>
 *
 * @author: wxm
 * @Date: 2021/10/6
 */
@AllArgsConstructor
public class JwtTokenClientUtil {
    /**
     *  从supplier中获取JWTAuthClient
     */
    private final Supplier<? extends JWTAuthClient> supplier;

    /**
     * 解析token
     * @param token Token
     * @param tClass 解析后的class类型
     * @param <T> T
     * @return T类型对象
     */
    public <T> T getUserInfo(String token,Class<T> tClass) {
        try {
            JWTAuthClient jwtAuthClient = supplier.get();
            PublicKey publicKey = jwtAuthClient.getPublicKey();
            return JwtHelper.getJwtFromToken(token,publicKey,tClass);
        }catch (Exception e){
            JWTException.error("公钥生成失败：" + e.getMessage());
        }
        return null;
    }
}
